<html>
    <head>
        <title>交易系统－－订单查询</title>
    </head>
    <body>
        <div id="parameterTabs" class="x-form-item">
        </div>
        <div id="queryOrderGrid" class="x-form-item">
        </div>
        <div id="queryTicketGrid" class="x-form-item">
        </div>
        <script>
            var webOrderDS;
            var ticketDS;
            Ext.onReady(function(){
                // second tabs built from JS
                tabs.getItem(0).setTitle('订单综合查询');
                var query_order_tabs = new Ext.TabPanel({
                    id: 'query_order_tabs',
                    renderTo: 'parameterTabs',
                    activeTab: 0,
                    width: '100%',
                    height: 60,
                    plain: true,
                    defaults: {
                        autoScroll: true
                    },
                    items: [{
                            id: 'query_order_tab1',
                            title: '订单号查询',
                            autoLoad: {
                                url: '/ticket/search_by_orderid',
                                scripts: true
                            }
                        }, {
                            id: 'query_order_tab2',
                            title: 'PNR查询',
                            autoLoad: {
                                url: '/ticket/search_by_pnr',
                                scripts: true
                            }
                        }, {
                            id: 'query_order_tab3',
                            title: '乘机人信息查询',
                            autoLoad: {
                                url: '/ticket/search_by_pass',
                                scripts: true
                            }
                        }, {
                            id: 'query_order_tab4',
                            title: '复杂查询',
                            autoLoad: {
                                url: '/ticket/search_by_other',
                                scripts: true
                            }
                        }]
                });


                //############## 订单Grid ##############
                var orderCM = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
                        header: '编号',
                        dataIndex: 'id'
                    }, {
                        header: '乘机人',
                        dataIndex: 'pass',
                        width: 120
                    }, {
                        header: '定单号',
                        dataIndex: 'order_no',
                        width: 120
                    }, {
                        header: 'PNR',
                        dataIndex: 'pnr',
                        width: 60
                    }, {
                        header: '航班号',
                        dataIndex: 'air_no',
                        width: 60
                    }, {
                        header: '始发地',
                        dataIndex: 'from',
                        width: 120
                    }, {
                        header: '目的地',
                        dataIndex: 'to',
                        width: 120
                    }, {
                        header: '乘机日期',
                        dataIndex: 'fly_date',
                        width: 90
                    }, {
                        header: '起飞(T)',
                        dataIndex: 'fly_time',
                        width: 50
                    }, {
                        header: '预定时间',
                        dataIndex: 'created_at',
                        width: 130
                    }, {
                        header: '支付状态',
                        dataIndex: 'trx_status',
                        width: 70
                    }, {
                        header: '订单状态',
                        dataIndex: 'status',
                        width: 70
                    },{
                        header: '操作',
                        width: 130,
                        dataIndex: 'fromto',
                        renderer: operationRender
                    }]);
                function operationRender(value, cellmeta, record, rowIndex, columnIndex, store){
                    var content = "";
                    var cancel_str = "&nbsp;<a href=# onclick='delWebOrder(\""+record.get("id")+"\")'>取消</a>";
                    if (record.get("trx_status")=="等待支付"){
                        content += "<a href=\"/ticket/agentReadyPay?web_order_no="+record.get("order_no")+"&fromto="+record.get("fromto")+"\" target=_blank>马上支付</a>||";
                        content += cancel_str;
                    }
                    if (record.get("status")=="正在处理" || record.get("status")=="支付成功"){
                        content += "<a href=# onclick='drawWebOrder(\""+record.get("id")+"\")'>申请退票</a>";
                    }
                    if (record.get("status")=="处理完成" || record.get("status")=="支付成功"){
                        content += "<a href=# onclick='drawWebOrder(\""+record.get("id")+"\")'>申请退票</a>";
                    }
                    return content
                }
                delWebOrder = function(id){
                    Ext.Ajax.request({
                        url: '/ticket/cancel_order',
                        params: 'id='+id,
                        method: 'GET',
                        success: function(result, request){
                            if (result.responseText.indexOf("成功") >= 0) {
                                Ext.MessageBox.alert('操作成功', result.responseText);
                                webOrderDS.reload();
                                ticketDS.baseParams.id = "";
                                ticketDS.reload();
                            }
                            else {
                                Ext.MessageBox.alert('操作失败', result.responseText);
                            }
                        },
                        failure: function(result, request){
                            Ext.MessageBox.alert('操作失败', '取消订单失败');
                        }
                    });
                }
                drawWebOrder = function(id){
                    Ext.Ajax.request({
                        url: '/ticket/draw_order',
                        params: 'id='+id,
                        method: 'GET',
                        success: function(result, request){
                            if (result.responseText.indexOf("成功") >= 0) {
                                Ext.MessageBox.alert('操作成功', result.responseText);
                                webOrderDS.reload();
                                ticketDS.baseParams.id = "";
                                ticketDS.reload();
                            }
                            else {
                                Ext.MessageBox.alert('操作失败', result.responseText);
                            }
                        },
                        failure: function(result, request){
                            Ext.MessageBox.alert('操作失败', '申请退票失败');
                        }
                    });
                }
                //隐藏order数据库id
                orderCM.setHidden(1, !orderCM.isHidden(1));
                webOrderDS = new Ext.data.Store({
                    proxy: new Ext.data.HttpProxy({
                        url: '/ticket/getWebOrdersBySearch'
                    }),
                    reader: new Ext.data.JsonReader({
                        totalProperty: 'totalProperty',
                        root: 'root'
                    }, [{
                            name: 'id'
                        }, {
                            name: 'pass'
                        }, {
                            name: 'order_no'
                        }, {
                            name: 'pnr'
                        }, {
                            name: 'trx_status'
                        }, {
                            name: 'status'
                        }, {
                            name: 'air_no'
                        }, {
                            name: 'from'
                        }, {
                            name: 'to'
                        }, {
                            name: 'fly_date'
                        }, {
                            name: 'fly_time'
                        }, {
                            name: 'created_at'
                        }, {
                            name: 'fromto'
                        }])
                });

                var ordergridpanel = new Ext.grid.GridPanel({
                    el: 'queryOrderGrid',
                    ds: webOrderDS,
                    cm: orderCM,
                    //sm: sm,
                    loadMask: true,
                    autoWidth: true,
                    collapsible: true,
                    animCollapse: true,
                    height: 300,
                    title: '订单信息',
                    bbar: new Ext.PagingToolbar({
                        pageSize: 10,
                        store: webOrderDS,
                        displayInfo: true,
                        displayMsg: '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
                        emptyMsg: "没有记录"
                    })

                });

                ordergridpanel.render();
                //ds.load();
                webOrderDS.load({
                    params: {
                        start: 0,
                        limit: 10
                    }
                });
                var ordersrow;
                var current_order_id;
                //双击订单查看机票详细
                ordergridpanel.on('rowdblclick', function(ordergridpanel, rowIndex, e){
                    var current_order_selected = webOrderDS.data.items[rowIndex].id;
                    ordersrow = webOrderDS.getById(current_order_selected);//ds为绑定到grid的数据源
                    current_order_id = ordersrow.get("id");
                    ticketDS.baseParams.id = current_order_id;
                    ticketDS.reload();
                });

                //### 机票Grid ###
                var ticketCM = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
                        header: '编号',
                        dataIndex: 'id',
                        hidden: true
                    }, {
                        header: 'PNR',
                        dataIndex: 'pnr',
                        width: 60
                    }, {
                        header: '旅客姓名',
                        dataIndex: 'pass_name',
                        width: 70
                    }, {
                        header: '证件号码',
                        dataIndex: 'pass_card_id',
                        width: 140
                    }, {
                        header: '票号',
                        dataIndex: 'ticket_no',
                        width: 110
                        /*
                    }, {
                        header: '状态',
                        dataIndex: 'status',
                        width: 50
                         */
                    }, {
                        header: '机票价格',
                        dataIndex: 'price',
                        width: 60
                    }, {
                        header: '机场费税',
                        dataIndex: 'airdrome_tax',
                        width: 60
                    }, {
                        header: '附加费',
                        dataIndex: 'fuel_tax',
                        width: 60
                        /*
                    }, {
                        header: '保险份数',
                        dataIndex: 'insurance_number',
                        width: 60
                         */
                    }, {
                        header: '其他',
                        dataIndex: 'other',
                        width: 50
                    }, {
                        header: '合计',
                        dataIndex: 'total_price',
                        width: 60
                    },{
                        header: '修改证件',
                        width: 70,
                        renderer: modCardRender
                    },{
                        header: '取消',
                        width: 60,
                        renderer: delRender
                    }]);
                ticketCM.defaultSortable = true;
                ticketDS = new Ext.data.Store({
                    proxy: new Ext.data.HttpProxy({
                        url: '/ticket/getTicketsByOrderID'
                    }),
                    reader: new Ext.data.JsonReader({
                        //totalProperty: 'totalProperty',
                        root: 'root'
                    }, [{
                            name: 'id'
                        }, {
                            name: 'pnr'
                        }, {
                            name: 'web_order_id'
                        }, {
                            name: 'pass_name'
                        }, {
                            name: 'pass_tel'
                        }, {
                            name: 'pass_card_id'
                        }, {
                            name: 'ticket_no'
                        }, {
                            name: 'price'
                        }, {
                            name: 'status'
                        }, {
                            name: 'airdrome_tax'
                        }, {
                            name: 'fuel_tax'
                        }, {
                            name: 'other'
                        }, {
                            name: 'total_price'
                        }, {
                            name: 'insurance_campany'
                        }, {
                            name: 'insurance_out_date'
                        }, {
                            name: 'insurance_number'
                        }, {
                            name: 'insurance_no'
                        }, {
                            name: 'insurance_type'
                        }])
                });

                var ticketgridpanel = new Ext.grid.GridPanel({
                    el: 'queryTicketGrid',
                    ds: ticketDS,
                    cm: ticketCM,
                    loadMask: true,
                    autoWidth: true,
                    collapsible: true,
                    animCollapse: true,
                    height: 180,
                    title: '乘客航班信息'
                });

                ticketgridpanel.render();
                ticketDS.load();

                function modCardRender(value, cellmeta, record, rowIndex, columnIndex, store){
                    if (record.get("status")=="处理完成"){
                        return ""
                    }else{
                        var cur_ticket_id = record.get("id");
                        return "<a href=# onclick='modWebPassCard(\""+cur_ticket_id+"\")'>修改证件号</a>";
                    }
                }
                function delRender(value, cellmeta, record, rowIndex, columnIndex, store){
                    if (record.get("status")=="处理完成"){
                        return "";
                    }else{
                        var cur_ticket_id = record.get("id");
                        return "<a href=# onclick='delTicket(\""+cur_ticket_id+"\")'>取消</a>";
                    }
                }
                delTicket = function(id){
                    Ext.Ajax.request({
                        url: '/ticket/cancel_ticket',
                        params: 'id='+id,
                        method: 'GET',
                        success: function(result, request){
                            if (result.responseText.indexOf("成功") >= 0) {
                                Ext.MessageBox.alert('操作成功', '取消乘客成功');
                                webOrderDS.reload();
                                ticketDS.baseParams.id = "";
                                ticketDS.reload();
                            }
                            else {
                                Ext.MessageBox.alert('操作失败', result.responseText);
                            }
                        },
                        failure: function(result, request){
                            Ext.MessageBox.alert('操作失败', '取消乘客失败');
                        }
                    });
                }
                modWebPassCard = function(id){
                    var id_card = new Ext.form.TextField({
                        fieldLabel: '证件号码',
                        name: 'pass_card_id',
                        allowBlank: true,
                        anchor: '90%'
                    });

                    var formPanel = new Ext.form.FormPanel({
                        //baseCls: 'x-plain',
                        labelWidth: 60,
                        labelAlign: 'right',
                        frame: true,
                        url: "/ticket/saveEditTicket?id=" + id,
                        reader: new Ext.data.JsonReader({
                            root: 'root'
                        }, ['pass_card_id']),
                        items: [id_card]
                    });

                    formPanel.form.load({
                        url: '/ticket/getEditTicketInfoByID?id=' + id,
                        waitMsg: '正在载入数据...',
                        success: function(form, action){
                            //Ext.MessageBox.alert('提示信息', '信息加载成功！');
                        },
                        failure: function(form, action){
                            Ext.MessageBox.alert('提示信息', '信息加载失败');
                        }
                    });

                    var fixWindow = new Ext.Window({
                        title: '编辑证件号码',
                        width: 260,
                        height: 130,
                        layout: 'fit',
                        plain: true,
                        bodyStyle: 'padding:5px;',
                        buttonAlign: 'center',
                        items: formPanel,
                        buttons: [{
                                text: '保存',
                                handler: function(){
                                    // check form value
                                    if (formPanel.form.isValid()) {
                                        formPanel.form.submit({
                                            waitMsg: 'In processing',
                                            success: function(form, action){
                                                var info = "操作失败，具体失败原因请查询系统日志";
                                                if (action.result && action.result.info && action.result.info != "") {
                                                    info = action.result.info;
                                                }
                                                if (info.indexOf("成功") >= 0) {
                                                    Ext.MessageBox.alert('编辑成功', info);
                                                    fixWindow.hide();
                                                    ticketDS.reload();
                                                }else{
                                                    Ext.MessageBox.alert('编辑失败', info);
                                                }
                                            },
                                            failure: function(form, action){
                                                Ext.MessageBox.alert('操作信息', '操作失败');
                                            }
                                        });
                                    }
                                }
                            }, {
                                text: '取消',
                                handler: function(){
                                    fixWindow.hide();
                                }
                            }]
                    });
                    fixWindow.show();
                }
            });
        </script>
    </body>
</html>
